'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Partial Public Class PerformanceWindow
    Private _timer As System.Windows.Threading.DispatcherTimer
    Private _process As System.Diagnostics.Process

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _timer = New System.Windows.Threading.DispatcherTimer
        _timer.Interval = TimeSpan.FromSeconds(1)

        AddHandler _timer.Tick, AddressOf OnTimerTick

        _process = System.Diagnostics.Process.GetCurrentProcess
        _timer.Start()

        App.DefaultWindowStyle.Attach(Me)
    End Sub

    Private Sub OnTimerTick(ByVal sender As Object, ByVal e As EventArgs)
        Dim memoryLoad As Long
        Dim cpuLoad As Double
        Dim threadCount As Integer
        Dim bytesSent As Long
        Dim bytesReceived As Long

        memoryLoad = _process.WorkingSet64
        cpuLoad = _process.TotalProcessorTime.TotalMilliseconds
        threadCount = _process.Threads.Count

        If ApplicationService.Current.Context IsNot Nothing Then
            Dim svConnect = DirectCast(ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IConnector)(), IcqInterface.IcqConnector)

            bytesSent = svConnect.TcpContext.BytesSent
            bytesReceived = svConnect.TcpContext.BytesReceived
        End If

        Me.MemoryUsage.Text = String.Format("{0:#,##0}", memoryLoad)
        Me.CpuUsage.Text = String.Format("{0:#,##0}", cpuLoad)
        Me.CurrentThreads.Text = String.Format("{0}", threadCount)
        Me.BytesSent.Text = String.Format("{0:#,##0}", bytesSent)
        Me.BytesReceived.Text = String.Format("{0:#,##0}", bytesReceived)
    End Sub

End Class
